#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>

#include "gtkutils.h"
#include "FinalCType/String.h"
#include "sqliteutils.h"

void sayHello() {
    printf("hello word\n");
}
Sqlite *db = NULL;

GtkListStore *list_store = NULL;
GtkComboBox *combo = NULL;
GtkEntry *entry = NULL;

createSignalObjectFunc(xdg_open_click, GtkComboBox) {
    GtkTreeIter iter;
    GtkTreeModel *model = getComboBoxModel(user_data);
    if (getComboBoxActiveIter(user_data, &iter)) { 
        char *text = NULL;
        treeModelGet(model, &iter, 0, &text);

        String *command = stringAppend_cstrs(NULL, "xdg-open", " ", text, NULL);

        system(command->value);

        deleteString(command);
    }
}

#define CONFIG_TABLE "xdg_open"
#define CONFIG_KEY_PATH "PATH"

void initDataBase() {
    String *path = stringAppend_cstrs(NULL, getenv("HOME"), "/", ".config/gtk_xdg_open.db", NULL);

    db = createSqliteDataBaseOfStringPath(path);
    deleteString(path);

    // Sql *sql = createTableSql(CONFIG_TABLE, "PAHT TEXT");
    Sql *sql = createTableSqlAfter(CONFIG_TABLE, 
                    stringAppend_cstrs(NULL, CONFIG_KEY_PATH, " TEXT", NULL), 
                        deleteString);
    
    sqliteExecuteSqlCallBack(db, sql, NULL);
}

createSqliteExecuteCallBackFunc(xdg_open_update_handle) {
    GtkTreeIter iter = {0};

    for (int i = 0; i < columns; i++) {
        String *title = createStringWith_cstr(column_titles[i]);
        if (stringCompare_cstr(title, CONFIG_KEY_PATH)) {
            printf("title: %s --> %s\n",title->value, column_data[i]);
            listStoreAppend(list_store, &iter);
            listStoreSet(list_store, &iter, 0, column_data[i]);
        }
        destroyString(title);
    }
    return 0;
}

void updateListStore() {
    listStoreClear(list_store);
    Sql *sql = createSelectSql(CONFIG_TABLE, CONFIG_KEY_PATH);
    printf("sql: %s\n",sql->sql->value);
    sqliteExecuteSqlCallBack(db, sql, xdg_open_update_handle);
    setComboBoxActive(combo, 0);
}

createSignalObjectFunc(saveDataBase, GtkEntry) {
    const char *data = entryGetText(user_data);
    printf("entryData: %s\n", data);

    if (data) {
        Sql *sql = createInsertSqlAfter(CONFIG_TABLE, CONFIG_KEY_PATH, 
                    stringAppend_cstrs(NULL, "\"",data, "\"", NULL), 
                        deleteString);

        sqliteExecuteSqlCallBack(db, sql, NULL);
        updateListStore();
    }
}

static void activate(GtkApplication *app, gpointer user_data) {
    GtkWindow *window;
    list_store = createListStore(1, G_TYPE_STRING);
    
    initDataBase();
    updateListStore();

    window = createWindowWithTitleSize(app, "gtk-xdg-open-test", 600, 400);

    combo = createComboBoxWithModel(convTreeModel(list_store));
    setCellLayout(combo, createCellRendererText(), "text", 0, NULL, true);
    setComboBoxActive(combo, 0);

    entry = createEntry();

    setWindowChild(window, 
        createBoxWithVerticalAppends(0, 
            createBoxWithHorizntalMarginAlignAppends(0,0,ACENTER, 
                createLabel("xdg-open:"),
                combo,
                createButtonAction(combo, "Xdg-Open", xdg_open_click),
                createButtonAction(combo, "Delete", NULL),
                NULL
            ),
            createBoxWithHorizntalMarginAlignAppends(0,0,ACENTER, 
                createLabel("创建条目:"),
                entry,
                createButtonAction(entry, "Add", saveDataBase),
                NULL
            ),
            NULL
        )
    );

    showWindow(window);
}

int	main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = createApplicationConnectActivate("gtk.example.template", 
                                G_APPLICATION_FLAGS_NONE, activate);
    // app = gtk_application_new("gtk.example.template", G_APPLICATION_FLAGS_NONE);
    // g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    // status = g_application_run(G_APPLICATION(app), argc, argv);
    // g_object_unref(app);
    status = runApplication(app, argc, argv);
    objectUnref(app);

    return status;
}

